/**
 * 
 */
package com.hoopchina.settings;

import com.hoopchina.news.R;
import com.hoopchina.ui.home.CityEngine;
import com.hoopchina.ui.home.HomeView;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * 设置：地区设置对话框
 * @author Guo Baiyu
 *
 */
public class CitySettingDialog extends DialogPreference {
	public final static String	 HTTP_DEFAULT_CITY	    = "北京";    // 默认设置城市
	public static final String	 PREFS_NAME	            = "MyPrefsFile";

	private String[]	         mProvinceNames	        = null;
	private int[]	             mProvinceCodes	        = null;

	// private static final String[]
	private Context	             mContext	            = null;
	private ArrayAdapter<String> mAdapterProvince		= null;
	private ArrayAdapter<String> mAdapterCity	    	= null;
	private String[]	         mCities	            = null;
	private String	             mCurrentProvince	    = null;
	private String	             mCurrentCity	        = null;
	private SharedPreferences	 mCitySharedPreferences	= null;

	/**
	 * @param context
	 * @param attrs
	 */
	public CitySettingDialog(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}
	
	
	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public CitySettingDialog(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	
	
	private void init(Context context) {
		mContext = context;
		mProvinceNames = mContext.getResources().getStringArray(R.array.a_settings_province_names);
		mProvinceCodes = mContext.getResources().getIntArray(R.array.a_settings_province_codes);
		mCitySharedPreferences = mContext.getSharedPreferences(PREFS_NAME, 0);  

	}
	
    void refresh() {
		mCurrentProvince = mCitySharedPreferences.getString("mCurrentProvinceNews", HTTP_DEFAULT_CITY);  
		mCurrentCity = mCitySharedPreferences.getString("mCurrentCityNews", HTTP_DEFAULT_CITY); 
    	/*mCurrentProvince = HTTP_DEFAULT_CITY; 
		mCurrentCity = HTTP_DEFAULT_CITY;*/
    }

    
	@Override
	public void onActivityDestroy() {
//		Toast.makeText(mContext, "onActivityDestroy", 200).show();
		super.onActivityDestroy();
	}


	@Override
	public void onClick(DialogInterface dialog, int which) {
		if (which == DialogInterface.BUTTON1) {
		 String mOldCity = mCitySharedPreferences.getString("mCurrentCityNews", ""); 
			if (!mOldCity.equals(mCurrentCity)) {
				HomeView.mRefreshLocalNewsKey=true;
				SharedPreferences.Editor editor = mCitySharedPreferences.edit();  
				editor.putString("mCurrentProvinceNews", mCurrentProvince);  
				//editor.putString("mCurrentCity", mCurrentCity);  
				editor.putString("mCurrentCityNews", mCurrentCity);  
				editor.commit();  
			}
			
			
		}
//		Toast.makeText(mContext, "onClick + dialog +which:"+which, 200).show();
		super.onClick(dialog, which);
	}


	@Override
	protected View onCreateDialogView() {
//		Toast.makeText(mContext, "onCreateDialogView", 200).show();

		LayoutInflater inflater = LayoutInflater.from(mContext);
		mAdapterProvince = new ArrayAdapter<String>(mContext,
				android.R.layout.simple_spinner_item, mProvinceNames);

		View layout = inflater.inflate(R.layout.dialogarea, null);
		if ( null == layout )
			return super.onCreateDialogView();
		final Spinner spinnerCity = (Spinner) layout.findViewById(R.id.spinnerCity);
		spinnerCity.setEnabled(false);

		Spinner spinnerProvince = (Spinner) layout.findViewById(R.id.spinnerProvince);
		// 设置下拉列表的风格
		mAdapterProvince.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		// 将adapter 添加到m_Spinner中
		// get preference  
		refresh();
		spinnerProvince.setAdapter(mAdapterProvince);
		
		for (int position = 0; position<mProvinceNames.length ; position++) {
			if ( mCurrentProvince.equals(mProvinceNames[position]) ) {
				spinnerProvince.setSelection(position, true);
				mCities = new CityEngine(mContext).getCitys(mProvinceCodes[position] , mProvinceCodes[position+1]);
				if ( 0 == mCities.length ){
					mCities = new String[]{mCurrentProvince};
				}
				mAdapterCity = new ArrayAdapter<String>(mContext,android.R.layout.simple_spinner_item,mCities);
				// 设置下拉列表的风格
				mAdapterCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				// 将adapter 添加到m_Spinner中
				spinnerCity.setAdapter(mAdapterCity);
				// set
				spinnerCity.setOnItemSelectedListener(new OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0,
							View arg1, int arg2, long arg3) {
						// TODO Auto-generated method stub
						mCurrentCity = mCities[arg2];
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {
						// TODO Auto-generated method stub
						
					}
				});
				break;
			}
		}
		
	
		
		
		
		
		//记录上次选择的位置
		int positionCity = 0;
		for (; positionCity < mCities.length ; positionCity++) {
			if ( mCurrentCity.equals(mCities[positionCity]) ) {
				break;
			}
		}
		if (positionCity>=mCities.length) {
			positionCity=0;
		}
		spinnerCity.setSelection(positionCity, true);

		spinnerCity.setEnabled(true);

		spinnerProvince.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0,
					View arg1, int arg2, long arg3) {
				mCurrentProvince = mProvinceNames[arg2];

				mCities = new CityEngine(mContext).getCitys(mProvinceCodes[arg2] , mProvinceCodes[arg2+1]);
				if ( 0 == mCities.length ){
					mCities = new String[]{mCurrentProvince};
				}
				mAdapterCity = new ArrayAdapter<String>(
						mContext,
						android.R.layout.simple_spinner_item,
						mCities);
				// 设置下拉列表的风格
				mAdapterCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
				// 将adapter 添加到m_Spinner中
				spinnerCity.setAdapter(mAdapterCity);

				spinnerCity.setEnabled(true);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
		});
		return layout;
	}

}
